package com.lb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lb.common.Result;
import com.lb.entity.DTO.LoginDTO;
import com.lb.entity.Student;
import com.lb.entity.admin;
import com.lb.service.adminService;
import com.lb.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping
@RestController
@CrossOrigin
@Slf4j
public class admincontroller {
   @Autowired
   private adminService adminservice;
   @GetMapping("/admin/info")
   public Result getInfo(@RequestParam String username)
   {
      QueryWrapper<admin> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("username", username);
      admin one = adminservice.getOne(queryWrapper);
      return Result.success(one);
   }
   @PostMapping("/admin/login")
   public Result login(@RequestBody LoginDTO loginDTO){
      QueryWrapper<admin> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("username",loginDTO.getUsername());
      queryWrapper.eq("password",loginDTO.getPassword());
      admin student = adminservice.getOne(queryWrapper);
      if (student == null)
      {
         return Result.error("失败");
      }
      Map<String,Object> map = new HashMap<>();
      map.put("id", student.getId());
      // map.put("name",user.getName());
      map.put("username", student.getUsername());
      String s = JwtUtils.generateJwt(map);
      String token= JwtUtils.generateJwt(map);
      System.out.println(token);
      return Result.success(token);
   }
}
